package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.contract.AddOrNotifyAddressContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerAddress;
import com.meitianhui.convenitenbuy.module.mine.model.Region;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.AddressDefaultEventBean;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.AddressEventBean;
import com.meitianhui.convenitenbuy.module.mine.presenter.AddOrNotifyAddressPresenter;
import com.meitianhui.convenitenbuy.util.GsonParserUtils;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.wheelview.RegionPopWindow;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by amy on 2017/11/13.
 */

public class AddOrNotifyAddressActivity extends BaseToolbarActivity implements AddOrNotifyAddressContract.View {

    private int mIntExtra;
    private ConsumerAddress.ConsumerData mConsumerAddressBean;
    private EditText mEtReceivingAddressName;
    private EditText mEtReceivingAddressNumber;
    private RelativeLayout mRlReceivingAddressProvinceCities;
    private EditText mEtReceivingAddressDetailAddress;
    private List<Region> regionList = new ArrayList<>();//地址的集合
    private List<Region.Children.ChildClass> childclassLists = new ArrayList<>();
    private List<Region.Children> childList = new ArrayList<>();
    @Inject
    AddOrNotifyAddressPresenter mAddOrNotifyAddressPresenter;
    private TextView mTvReceivingAddressProvinceCities;
    private String mTvAddressCode;
    private String mIsMajorAddress = "N";
    private Button mBtnReceivingAddressSave;
    private boolean mIsMajor;
    private String mReceivingAddressDetailAddress;

    @Override
    protected int setView() {
        return R.layout.activity_address_add_or_notify;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);
        isEnable(false);
        initTitle();
        initId();
        initRXClick();
        getUserReceivingAddress();

    }

    private void getUserReceivingAddress() {
        Type type = new TypeToken<List<Region>>() {
        }.getType();
        Object data = GsonParserUtils.getInstance().parserRegionJson(this, R.raw.city_change, type);
        List<Region> regions = (List<Region>) data;
        regionList.addAll(regions);
        if (mConsumerAddressBean != null) {
            //修改地址
            mEtReceivingAddressName.setText(mConsumerAddressBean.getConsignee());
            mEtReceivingAddressNumber.setText(mConsumerAddressBean.getMobile());
            mEtReceivingAddressDetailAddress.setText(mConsumerAddressBean.getAddress());
            mIsMajorAddress = mConsumerAddressBean.getIs_major_addr();
            mTvAddressCode = mConsumerAddressBean.getArea_id();
            mAddOrNotifyAddressPresenter.changeCode2Address(mTvAddressCode, regionList);
        }


    }

    @Override
    public void showAddress(String AfterChangeAddress) {
        mTvReceivingAddressProvinceCities.setText(AfterChangeAddress);
    }

    @Override
    public void showSucc(String str) {
        TipUtils.showToast(App.getInstance(), str, 3);
    }

    private void initTitle() {
        Intent intent = getIntent();
        mIntExtra = intent.getIntExtra(JumpUtil.JUMP_ADDRESS_CHANGE, 0);
        mIsMajor = intent.getBooleanExtra("isMajor", false);
        if (mIntExtra == 0) {
            setTbTitle("新增地址");
        } else {
            setTbTitle("编辑收货地址");
            mConsumerAddressBean = (ConsumerAddress.ConsumerData) intent.getSerializableExtra("data");
        }
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.rl_receiving_address_province_cities:
                SystemUtils.hidKeyBoard(this);
                mAddOrNotifyAddressPresenter.selectReceivingAddress(regionList, childclassLists, childList);
                //选择地址
                break;
            case R.id.btn_receiving_address_save:
                //保存地址
                String receivingAddressName = mEtReceivingAddressName.getText().toString().trim();
                if (TextUtils.isEmpty(receivingAddressName)) {
                    TipUtils.showToast(App.getInstance(), "请输入收货姓名");
                    return;
                }
                String receivingAddressNumber = mEtReceivingAddressNumber.getText().toString().trim();
                if (TextUtils.isEmpty(receivingAddressName)) {
                    TipUtils.showToast(App.getInstance(), "请输入收货人联系电话");
                    return;
                }
                if (!StringUtils.isPhone(receivingAddressNumber)) {
                    TipUtils.showToast(App.getInstance(), "请输入正确的收货人联系电话");
                    return;
                }

                if (TextUtils.isEmpty(mTvAddressCode)) {
                    TipUtils.showToast(App.getInstance(), "请选择省市区");
                    return;
                }
                mReceivingAddressDetailAddress = mEtReceivingAddressDetailAddress.getText().toString().trim();
                if (TextUtils.isEmpty(mReceivingAddressDetailAddress)) {
                    TipUtils.showToast(App.getInstance(), "请输入详细地址");
                    return;
                }

                //保存地址
                mAddOrNotifyAddressPresenter.saveReceivingAddress(mConsumerAddressBean, receivingAddressName, receivingAddressNumber, mTvAddressCode, mReceivingAddressDetailAddress, mIsMajorAddress);

                break;
        }
    }

    @Override
    protected void onRefreshData() {

    }

    private void initRXClick() {
        RxUtils.bindEvents(mEtReceivingAddressDetailAddress, this);
        RxUtils.bindEvents(mRlReceivingAddressProvinceCities, this);
        RxUtils.bindEvents(mEtReceivingAddressName, this);
        RxUtils.bindEvents(mEtReceivingAddressNumber, this);
        RxUtils.bindEvents(mBtnReceivingAddressSave, this);
    }

    private void initId() {
        mEtReceivingAddressName = getViewById(R.id.et_receiving_address_name);
        mEtReceivingAddressNumber = getViewById(R.id.et_receiving_address_number);
        mEtReceivingAddressDetailAddress = getViewById(R.id.et_receiving_address_detail_address);
        mRlReceivingAddressProvinceCities = getViewById(R.id.rl_receiving_address_province_cities);
        mTvReceivingAddressProvinceCities = getViewById(R.id.tv_receiving_address_province_cities);
        mBtnReceivingAddressSave = getViewById(R.id.btn_receiving_address_save);
    }


    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(), msg, 2);
    }

    @Override
    public void showAddressPopupWindow(List<Region> regionList, List<Region.Children> childList, List<Region.Children.ChildClass> childclassLists) {
        TextView tv_addressCode = new TextView(this);
        RegionPopWindow regionPopWindow = new RegionPopWindow(this, regionList, childList, childclassLists, mTvReceivingAddressProvinceCities, tv_addressCode, new RegionPopWindow.OnViewClickListener() {
            @Override
            public void ConfirmClick() {
                mTvAddressCode = tv_addressCode.getText().toString();
            }
        });
        regionPopWindow.show(mRlReceivingAddressProvinceCities);
    }

    /**
     * 跟新数据成功
     *
     * @param b
     */
    @Override
    public void addOrNotifySuccess(boolean b) {
        this.finish();
        //并通知收货地址去刷新页面
        RxBus.get().post(new AddressEventBean(Constants.ADDRESS_NOTIFY_RA_ACTIVITY));
        //通知mineFragment去更新地址
        if (mIsMajor) {
            RxBus.get().post(new AddressDefaultEventBean(mTvReceivingAddressProvinceCities.getText().toString()));
        }
        //如果是新增地址也要通知mineFragment去刷新
        if (b) {
            RxBus.get().post(new AddressDefaultEventBean(mTvReceivingAddressProvinceCities.getText().toString()));
        }
    }


}
